<?php

class db {

    public $conn = NULL;
    public $result = NULL;
    public $host = "localhost";
    public $user = "root";
    public $pass = "root";
    public $database = "schedule";

    /////// kết nối database/////
    function __construct() {
        $this->conn = mysql_connect($this->host, $this->user, $this->pass);
        mysql_select_db($this->database, $this->conn);
        mysql_query("set names 'utf8'");
    }

    /////// Tiếp nhận kết quả từ database////////
    function getdata($sql) {
        $this->result = mysql_query($sql);
    }

    /////////// Trích từng dòng database///////////////
    function fetchRow() {
        if (is_resource($this->result) == false)
            return false;
        $row = mysql_fetch_assoc($this->result);
        return $row;
    }

    function TheLoai($lang = 'vi', $AnHien = 1) {
        $sql = "SELECT idTL, TenTL from theloai WHERE (AnHien=$AnHien or $AnHien=-1)
			  AND (lang = '$lang' or '$lang'='') 
			  ORDER BY ThuTu";
        $kq = mysql_query($sql) or die(mysql_error());
        return $kq;
    }

//TheLoai

    function LoaiTin($lang = 'vi', $AnHien = 1) {
        $sql = "SELECT idLT, Ten  FROM loaitin
			WHERE (AnHien = $AnHien or $AnHien = -1) 
			AND (lang = '$lang' or '$lang'='') 
			ORDER BY Thutu";
        $kq = mysql_query($sql) or die(mysql_error());
        return $kq;
    }

//LoaiTin	

    function LoaiTinTrongTheLoai($idTL, $AnHien = 1) {
        settype($idTL, "int");
        $sql = "SELECT idLT, Ten,Ten_KhongDau FROM loaitin  WHERE (AnHien=$AnHien or $AnHien=-1)  
		  AND	idTL=$idTL ORDER BY ThuTu ASC";
        $kq = mysql_query($sql) or die(mysql_error());
        return $kq;
    }

//LoaiTinTrongTheLoai

    function changeTitle($str) {
        $str = $this->stripUnicode($str);
        $str = str_replace("?", "", $str);
        $str = str_replace("&", "", $str);
        $str = str_replace("'", "", $str);
        $str = str_replace("  ", " ", $str);
        $str = trim($str);
        $str = mb_convert_case($str, MB_CASE_TITLE, 'utf-8');
        // MB_CASE_UPPER/MB_CASE_TITLE/MB_CASE_LOWER
        $str = str_replace(" ", "-", $str);
        return $str;
    }

    function stripUnicode($str) {
        if (!$str)
            return false;
        $unicode = array(
            'a' => 'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ',
            'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
            'd' => 'đ',
            'D' => 'Đ',
            'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
            'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
            'i' => 'í|ì|ỉ|ĩ|ị',
            'I' => 'Í|Ì|Ỉ|Ĩ|Ị',
            'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
            'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
            'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
            'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
            'y' => 'ý|ỳ|ỷ|ỹ|ỵ',
            'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
        );
        foreach ($unicode as $khongdau => $codau) {
            $arr = explode("|", $codau);
            $str = str_replace($arr, $khongdau, $str);
        }
        return $str;
    }

    function DemSoTinTrong1Loai($idTL = -1, $idLT = -1, $lang = 'vi', $AnHien = -1) {
        $sql = "SELECT count(*) FROM  tin 
			WHERE (idTL=$idTL OR $idTL=-1) AND (idLT=$idLT OR $idLT=-1) 
			AND (lang='$lang' OR '$lang'='') AND (AnHien=$AnHien OR $AnHien=-1) ";
        $rs = mysql_query($sql) or die(mysql_error());
        $rows_rs = mysql_fetch_row($rs);
        $totalRows = $rows_rs[0];
        return $totalRows;
    }

//Đếm số tin trong một loại

    function pagesLink($totalRows, $pageNum = 1, $pageSize = 5) {
        $baseURL = $_SERVER['PHP_SELF'];
        parse_str($_SERVER['QUERY_STRING'], $arr);
        unset($arr['pageNum']);
        $str = "";
        foreach ($arr as $k => $v)
            $str.= "&{$k}={$v}";
        $baseURL .="?" . $str;

        if ($totalRows <= 0)
            return "";
        $totalPages = ceil($totalRows / $pageSize);
        if ($totalPages <= 1)
            return "";

        $firstLink = "";
        $prevLink = "";
        $lastLink = "";
        $nextLink = "";

        if ($pageNum > 1) {
            $firstLink = "<a href='$baseURL'>Trang đầu</a>";
            $prevPage = $pageNum - 1;
            $prevLink = "<a href='$baseURL&pageNum=$prevPage'>Trang trước</a>";
        }
        if ($pageNum < $totalPages) {
            $lastLink = "<a href='$baseURL&pageNum=$totalPages'>Trangcuối</a>";
            $nextPage = $pageNum + 1;
            $nextLink = "<a href='$baseURL&pageNum=$nextPage'>Trang kế</a>";
        }
        return $firstLink . $prevLink . $nextLink . $lastLink;
    }

//Page Link

    function pagesList($totalRows, $pageNum = 1, $pageSize = 5, $offset = 5) {
        $str = "";
        $baseURL = $_SERVER['PHP_SELF'];
        parse_str($_SERVER['QUERY_STRING'], $arr);
        unset($arr['pageNum']);
        foreach ($arr as $k => $v)
            $str.= "&{$k}={$v}";
        $baseURL .="?" . $str;



        if ($totalRows <= 0)
            return "";
        $totalPages = ceil($totalRows / $pageSize);
        if ($totalPages <= 1)
            return "";

        $firstLink = "";
        $prevLink = "";
        $lastLink = "";
        $nextLink = "";

        if ($pageNum > 1) {//n?u user ?ang ? trang th? 2 tr? ?i
            $firstLink = "<li><a href='$baseURL'>&laquo;</a></li>";
            $prevPage = $pageNum - 1;
            $prevLink = "<li><a href='$baseURL&pageNum=$prevPage'>Trang trước</a></li>";
        }
        if ($pageNum < $totalPages) { //user ch?a ??n trang cu?i
            $lastLink = "<li><a href='$baseURL&pageNum=$totalPages'>&raquo;</a></li>";
            $nextPage = $pageNum + 1;
            $nextLink = "<li><a href='$baseURL&pageNum=$nextPage'>Trang kế</a></li>";
            //có th? thay th? ch? b?ng hình,th? label (Trang k?, Trang cu?i...) là <img src='tenhinh.jpg'/>
        }

        $from = $pageNum - $offset;
        $to = $pageNum + $offset;
        if ($from <= 0) {
            $from = 1;
            $to = $offset * 2;
        }
        if ($to > $totalPages) {
            $to = $totalPages;
        }
        $links = "";
        for ($j = $from; $j <= $to; $j++) {
            if ($j != $pageNum)
                $links = $links . "<li><a href = '$baseURL&pageNum=$j'>$j</a></li>";
            else
                $links = $links . "<li class='active'><span>$j</span></li>";
        } //for
        return $firstLink . $links . $lastLink;
    }

//pagesList

    function pagesList1($baseURL, $totalRows, $pageNum = 1, $pageSize = 5, $offset = 5) {
        if ($totalRows <= 0)
            return "";
        $totalPages = ceil($totalRows / $pageSize);
        if ($totalPages <= 1)
            return "";

        $from = $pageNum - $offset;
        $to = $pageNum + $offset;
        if ($from <= 0) {
            $from = 1;
            $to = $offset * 2;
        }
        if ($to > $totalPages) {
            $to = $totalPages;
        }
        $links = "";
        for ($j = $from; $j <= $to; $j++) {
            $links = $links . "<a href = '$baseURL/$j/'>$j</a>";
        } //for
        return $links;
    }

// function pagesList

    function ChuoiNgauNhien($sokytu) {
        $chuoi = "ABCDEFGHIJKLMNOPQRSTUVWXYZWabcdefghijklmnopqrstuvwxyzw0123456789";
        for ($i = 0; $i < $sokytu; $i++) {
            $vitri = mt_rand(0, strlen($chuoi));
            $giatri = $giatri . substr($chuoi, $vitri, 1);
        }
        return $giatri;
    }

}

?>